<template>
  <el-dialog title="分配角色" :visible="showAssign" @close="isshowAssign">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="isshowAssign">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById, putAssignRoles } from '@/api/user'

export default {
  props: {
    showAssign: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [], // 角色列表
      checkList: [],
      id: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 发送请求
    async btnOK() {
      await putAssignRoles({ id: this.id, roleIds: [...this.checkList] })
      this.$message.success('保存完成')
      this.isshowAssign()
    },
    // 根据ID获取员工信息
    async getUserDetailById(id) {
      this.id = id
      const { roleIds } = await getUserDetailById(id)
      console.log(38, roleIds)
      this.checkList = roleIds
    },
    // 获取所有角色
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    // 关闭弹窗
    isshowAssign() {
      this.checkList = []
      this.$emit('isshowAssign')
    }
  }
}
</script>

<style>

</style>
